package cool.tianyou.blue.bird.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Icon;
import android.os.Build;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.util.Log;

import androidx.annotation.RequiresApi;

import cool.tianyou.blue.bird.R;
import cool.tianyou.blue.bird.factory.MessageFactory;
import cool.tianyou.blue.bird.model.CommonMessage;
import cool.tianyou.blue.bird.model.NotificationData;
import cool.tianyou.blue.bird.util.Util;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;

public class SMSReceiver extends BroadcastReceiver {
    static Context context;

    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    public void onReceive(Context context, Intent intent) {
        SMSReceiver.context = context;
        Bundle bundle = intent.getExtras();
        SmsMessage msg;
        if (null != bundle) {
            Object[] smsObj = (Object[]) bundle.get("pdus");
            StringBuilder builder = new StringBuilder();
            String address = "";

            Date date = null;
            for (Object object : smsObj) {
                msg = SmsMessage.createFromPdu((byte[]) object);
                date = new Date(msg.getTimestampMillis());// 时间
                builder.append(msg.getDisplayMessageBody());
                address = msg.getOriginatingAddress();

            }
            Util.sendMessage(MessageFactory.smsMessage(address, date, builder.toString(), context), context);
        }
    }


}
